<template>
  <el-dialog v-model="visible" :title="`${!dataForm.id ? $t('add') : $t('update')}访客信息`" :close-on-click-modal="false"
    :close-on-press-escape="false" width="600">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
      label-width="120px">
      <TitleBar title="访客信息" />
      <el-form-item :label="$t('visitor.name')" prop="personId" required>
        <el-select v-model="dataForm.personId" placeholder="请选择" @change="handleSelect" :disabled="dataForm.id">
          <el-option v-for="item in visitorList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('visitor.effectiveTime')" prop="time" required>
        <el-date-picker v-model="dataForm.time" type="datetimerange" :start-placeholder="$t('visitor.effectiveTime')"
          :end-placeholder="$t('visitor.expiryTime')" value-format="YYYY-MM-DD HH:mm:ss" />
      </el-form-item>
      <el-form-item :label="$t('visitor.reason')" prop="reason" required>
        <el-input v-model="dataForm.reason" placeholder="请输入"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  name: "",
  faceId: "",
  gender: null,
  personId: "",
  effectiveTime: '',
  expiryTime: '',
  time: [],
  reason: '',
});

const rules = ref({
  personId: [{ required: true, message: t("validate.required"), trigger: "change" }],
  time: [{ required: true, message: t("validate.required"), trigger: "change" }],
  reason: [{ required: true, message: t("validate.required"), trigger: "blur" }],
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  Promise.all([getVisitorList()]).then(() => {
    if (id) {
      getInfo(id);
    }
  });
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/visitor/info/" + id).then((res) => {
    Object.assign(dataForm, res.data, {time: [res.data.effectiveTime, res.data.expiryTime]});
  });
};

// 获取访客列表
const visitorList = ref<any[]>([]);
const getVisitorList = () => {
  baseService.get("/data/personnel/visitor/page").then((res) => {
    visitorList.value = res.data.list;
  });
};

const handleSelect = (val: string) => {
  const info = visitorList.value.find((d: any) => d.id === val) || {};
  dataForm.name = info.name;
  dataForm.faceId = info.faceId;
  dataForm.gender = info.gender;
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    baseService[!dataForm.id ? 'post' : 'put'](`/visitor/info/${!dataForm.id ? 'save' : ''}`, {
      ...dataForm,
      effectiveTime: dataForm.time[0],
      expiryTime: dataForm.time[1],
    }).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
